class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> v;
        sort(nums.begin(), nums.end());
        int sz = nums.size();
        for (int i = 0; i < sz; i++) {
            if (i && nums[i - 1] == nums[i])
                continue;
            int l = i + 1, r = sz - 1;
            while (l < r) {
                int sum = nums[i] + nums[l] + nums[r];
                if (sum < 0) {
                    ++l;
                    continue;
                } else if (sum > 0) {
                    --r;
                    continue;
                }
                v.push_back({nums[i], nums[l], nums[r]});
                do {
                    ++l;
                } while (l < r && nums[l - 1] == nums[l]);
                do {
                    --r;
                } while (l < r && nums[r + 1] == nums[r]);
            }
        }
        return v;
    }
};